﻿using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Itech_Run.Domain.Shared.Base;

namespace Itech_Run.Sys.Domain.Entity
{

    public partial class Sys_User : CustomAggregateRoot<string>
    {

        public int Category { get; set; }

        [Required]
        [StringLength(50)]
        public string UserName { get; set; }

        [StringLength(50)]
        public string? Name { get; set; }

        /// <summary>
        /// 1 男;   2 女
        /// </summary>

        public int? Sex { get; set; }

        [Column(TypeName = "dateTime")]
        public DateTime? Birthdate { get; set; }

        [StringLength(50)]
        public string? Nickname { get; set; }

        [StringLength(20)]
        public string? MobileNo { get; set; }

        [StringLength(50)]
        public string? Email { get; set; }

        [StringLength(200)]
        public string? PersonalImage { get; set; }

        [StringLength(50)]
        public string? Password { get; set; }

        [StringLength(100)]
        public string? Remark { get; set; }

        public bool IsActive { get; set; }

        public int Sort { get; set; }

        public virtual ICollection<Sys_UserRole> Sys_UserRole { get; set; } = new HashSet<Sys_UserRole>();

        public virtual ICollection<Sys_User_RefreshToken> Sys_User_RefreshToken { get; } = new HashSet<Sys_User_RefreshToken>();

        public virtual Sys_Resume Sys_Resume { get; set; }

        #region action

        public void AllocateUserRole(List<string> roleList)
        {
            Sys_UserRole = Sys_UserRole.Where(p => roleList.Contains(p.RoleId)).ToList();
            roleList = roleList.Where(p => !Sys_UserRole.Any(ur => ur.RoleId == p)).ToList();

            roleList.ForEach(item =>
            Sys_UserRole.Add(new Sys_UserRole
            {
                UserId = this.Id,
                RoleId = item,
                CreatedBy = this.LastModifiedBy,
                CreatedTime = this.LastModifiedTime,
                LastModifiedBy = this.LastModifiedBy,
                LastModifiedTime = this.LastModifiedTime
            }));
        }
        #endregion

    }
}
